//
//  NetworkTools.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/10.
//  Copyright © 2016年 sher. All rights reserved.
//

import AFNetworking

/*
 1.通过shareInstance拿到的永远是一个实例
 2.不管什么，永远只要一个实例
 */

enum RequestTyle : Int{
    case get = 0
    case post = 1
}

class NetworkTools: AFHTTPSessionManager {
    //let是线程安全的
    //单例
    static let shareInstance : NetworkTools = {
        let tools = NetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
}

// MARK:- 封装请求方法
extension NetworkTools{
    
    func request(methodTyle : RequestTyle, urlString : String, parameters : [String : AnyObject], finished : @escaping (_ result : AnyObject?, _ error : Error?) -> ()) -> Void {
        
        //1.定义成功的回调闭包
        let successCallBack = { (task : URLSessionDataTask, result : Any?) in
            
            finished(result as AnyObject?, nil)
        }
        //2.定义失败的回调闭包
        let failureCallBack = { (task : URLSessionDataTask?, error : Error) in
            
            finished(nil, error)
        }
        //3.发送网络请求
        if methodTyle == .get {
            
            get(urlString, parameters: parameters, progress: { (nil) in
                
                }, success: successCallBack, failure: failureCallBack)
        }else{
            
            post(urlString, parameters: parameters, progress: { (nil) in
                
                }, success: successCallBack, failure: failureCallBack)
        }
        
    }
    
}

// MARK:- 请求AccessToken
extension NetworkTools{
    
    func loadAccessToken(code : String, finished : @escaping (_ result : [String : AnyObject]?, _ error : Error?) -> ()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters  = ["client_id" : app_key, "client_secret" : app_secret, "grant_type" : "authorization_code", "code" : code, "redirect_uri" : redirect_url]
        
        request(methodTyle: .post, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            
            finished(result as? [String : AnyObject], error)
        }
        
        
        
    }
}

// MARK:- 请求用户信息
extension NetworkTools{
    func loadUserInfo(access_token : String, uid : String, finished : @escaping (_ resulet : [String : AnyObject]?, _ error : Error?) -> ()){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let parameters = ["access_token" : access_token, "uid" : uid]
        
        request(methodTyle: .get, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            
            finished(result as? [String : AnyObject], error)
            
        }
        
    }
}

// MARK:- 请求首页数据
extension NetworkTools{
    func loadStatuses(since_id : Int, max_id : Int, finished : @escaping (_ result : [[String : AnyObject]]?, _ error : Error?) -> ()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let parameters = ["access_token" : (PersonViewModel.shareIntance.account?.access_token)!, "since_id" : "\(since_id)", "max_id" : "\(max_id)"]
        
        request(methodTyle: .get, urlString: urlString, parameters: parameters as [String : AnyObject]) { (result, error) in
            
            guard let resultDict = result as? [String : AnyObject] else{
                
                finished(nil, error)
                return
            }
            
            print("")
            
            finished(resultDict["statuses"] as? [[String : AnyObject]], error)
            
        }
        
        
    }
}



